package com.my.datastructure;

public class SingleLinkedList {

    private final HeroNode head = new HeroNode(0, "", "");

    public void add(HeroNode node) {
        HeroNode temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = node;
    }

    public void list() {
        if (head.next == null) {
            throw new RuntimeException("当前链表为空");
        }
        HeroNode temp = head.next;
        while (temp != null) {
            System.out.println(temp);
            temp = temp.next;
        }
    }

    /**
     * 单链表的反转
     */
    public void reverse() {
        //定义一个新的链表reverseHead
        HeroNode reverseHead = new HeroNode(0, "", "");
        //每遍历一个节点，将节点取出，插入到新链表的最前端
        HeroNode temp = head.next;
        while (temp != null) {
            //取出第一个节点：将旧链表的头节点指向第二个节点
            head.next = temp.next;
            //当前节点指向新链表的第一个节点
            temp.next = reverseHead.next;
            //新链表的头结点指向当前节点
            reverseHead.next = temp;
            //指针下移
            temp = head.next;
        }
        head.next = reverseHead.next;
    }
}